package cn.qyd.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

/**
 * @author 邱运铎
 * @date 2024-03-21 15:55
 */
public class LockSupportPrintABC {
    private static Thread t1, t2, t3;
    public static void main(String[] args) throws InterruptedException {
        t1 = new Thread(() -> {
           for (int i = 0; i < 10; i++) {
               LockSupport.park();
               //LockSupport被中断不会抛出异常，如果需要判断是否被中断唤醒需要自己做判断
//               System.out.println(Thread.currentThread().isInterrupted());
               System.out.println("A");
               LockSupport.unpark(t2);
           }
        });

        t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.println("B");
                LockSupport.unpark(t3);
            }
        });

        t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                LockSupport.park();
                System.out.println("C");
                LockSupport.unpark(t1);
            }
        });
        t1.start();
        t2.start();
        t3.start();
        //这里让主线程稍微等一会保证三个线程都进入park状态
        Thread.sleep(100);

        //这里启动t1，
        LockSupport.unpark(t1);

    }
}
